// @algorithm @lc id=917 lang=cpp 
// @title boats-to-save-people


// @test([1,2],3)=1
// @test([3,2,2,1],3)=3
// @test([3,5,3,4],5)=4
class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(), people.end());
        auto L=people.begin(), W=L+people.size()-1;
        int boat = 0;
        while(L<=W){
            if(*L + *W <= limit)
                L++;
            W--;
            boat++;
        }
        return boat;
    }
};